// NewsDetailActivity.java
package com.example.farmer.Seller_Home;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.farmer.Database.ClassPostDatabase;
import com.example.farmer.Entity.ClassPost;
import com.example.farmer.R;

import java.util.Date;

public class NewsDetailActivity extends Activity {
    private ImageButton backbutton;

    private WebView newsWebView;
    private ClassPostDatabase db;
    private String newsTitle; // 用于存储从Intent传递的新闻标题

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        newsWebView = (WebView) findViewById(R.id.webViewNews);
        newsWebView.getSettings().setJavaScriptEnabled(true);
        newsWebView.setWebViewClient(new WebViewClient()); // 确保点击链接时不离开应用

        // 初始化数据库帮助类
        db = ClassPostDatabase.getInstance(this);

        // 从Intent获取新闻标题
        newsTitle = getIntent().getStringExtra("NEWS_TITLE");
        if (newsTitle != null && !newsTitle.isEmpty()) {
            loadNewsDetailByTitle(newsTitle);

            backbutton = findViewById(R.id.back_button);
            backbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                // 使用Main2Activity的包名来保证action的唯一性
//                String action = getPackageName() + ".ACTION_SHOW_CIRCLE_FRAGMENT";
//                Intent intent = new Intent(action);
//                intent.setClass(ClassPostActivity.this, MainActivity.class);
//                startActivity(intent);
                    finish();// 结束当前活动并返回
                }
            });
        }
    }

    private void loadNewsDetailByTitle(String title) {
        ClassPost post = db.getClassPostByTitle(title);
        if (post != null) {
            // 将新闻详情转换为HTML字符串
            String html = "<html><head><title>" + post.getTitle() + "</title></head><body>"
                    + "<h1>" + post.getTitle() + "</h1>"
                    + "<h6>" + "类型：" + post.getType() + "</h6>"
                    + "<h3><p>" + post.getContent() + "</p></h3>"
                    + (post.getImageUri() != null && !post.getImageUri().isEmpty()
                    ? "<img src='" + post.getImageUri() + "' alt='image' style='width:100%;' />" : "")
                    + "<h5>" +"发布地址："+ post.getAddress() + "</h5>"
                    + "<h6>" + new Date() + "</h6>"
                    + "</body></html>";

            // 将HTML加载到WebView
            newsWebView.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
        } else {
            // 没有找到新闻详情
            newsWebView.loadData("<html><body><p>新闻详情未找到</p></body></html>", "text/html", "UTF-8");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (db != null) {
            db.close(); // 关闭数据库
        }
    }
}
